package com.iwjw.ibatis.mapper;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * 封装Mybatis通用方法
 *
 * @author weiwei.huang
 * @Time 2016/10/19
 */
@AllArgsConstructor
@NoArgsConstructor
public enum SqlMethod {

    /*
    增
     */
    INSERT("insert", "插入单条数据", "<script>INSERT INTO %s %s VALUES %s</script>"),

    //https://my.oschina.net/u/2284972/blog/492516(要做foreach的对象，作为入参时，List<?>对象默认用list代替作为键，数组对象有array代替作为键)
    BATCH_INSERT("batchInsert", "插入单条数据", "<script>INSERT INTO %s %s VALUES \n<foreach item=\"item\" index=\"index\" collection=\"list\" separator=\",\">%s\n</foreach></script>"),

    INSERT_SELECTIVE("insertSelective", "插入单条数据（选择字段，null字段不插入）", "<script>INSERT INTO %s %s VALUES %s</script>"),
    
    /*
    删
     */
    DELETE_BY_PRIMARY_KEY("deleteByPrimaryKey", "根据主键ID删除", "DELETE FROM %s WHERE %s = %s"),

    DELETE_BY_PRIMARY_KEYS("deleteByPrimaryKeys", "根据逐渐IDS批量删除", "<script>DELETE FROM %s WHERE %s in (%s)</script>"),

    DELETE_BY_QUERY("deleteByQuery", "根据Query条件删除记录", "<script>DELETE FROM %s %s</script>"),

    /*
    改
     */
    //表名 更新字段 id字段 id值 乐观条件
    UPDATE_BY_PRIMARY_KEY("updateByPrimaryKey", "根据主键修改一条数据（可支持乐观锁）", "<script>update %s  %s WHERE %s = %s %s</script>"),

    UPDATE_BY_PRIMARY_KEY_NO_OPTIMISTIC("updateByPrimaryKeyNoOptimistic", "根据主键修改一条数据（非乐观）", "<script>update %s  %s WHERE %s = %s %s</script>"),

    UPDATE_BY_PRIMARY_KEY_SELECTIVE("updateByPrimaryKeySelective", "根据逐渐修改一条数据（选择字段，null字段不修改）", "<script>update %s  %s WHERE %s = %s %s</script>"),

    UPDATE_BY_PRIMARY_KEY_SELECTIVE_NO_OPTIMISTIC("updateByPrimaryKeySelectiveNoOptimistic", "根据逐渐修改一条数据（非乐观，选择字段，null字段不修改）", "<script>update %s  %s WHERE %s = %s %s</script>"),

    UPDATE_BY_QUERY("updateByQuery", "根据Query查询条件，更新记录", "<script>UPDATE %s %s %s</script>"),

    UPDATE_BY_QUERY_SELECTIVE("updateByQuerySelective", "根据Query查询条件，更新记录（选择字段，null字段不修改）", "<script>UPDATE %s %s %s</script>"),


    /*
    查
     */
    SELECT_BY_PRIMARY_KEY("selectByPrimaryKey", "根据主键查询一条数据", "<script>SELECT %s FROM %s WHERE %s = %s</script>"),

    SELECT_BY_PRIMARY_KEY_SPECIFIED_COLUMN("selectByPrimaryKeySpecifiedColumn", "根据主键查询一条数据（可指定返回字段）", "<script>SELECT %s FROM %s WHERE %s = %s</script>"),

    SELECT_BY_PRIMARY_KEYS("selectByPrimaryKeys", "根据主键ids查询数据", "<script>SELECT %s FROM %s where %s in (%s)</script>"),
    
    SELECT_BY_PRIMARY_KEYS_SPECIFIED_COLUMN("selectByPrimaryKeysSpecifiedColumn", "根据主键ids查询数据（可指定返回字段）", "<script>SELECT %s FROM %s where %s in (%s)</script>"),

    // 字段 表名 where语句 "order by"
    SELECT_BY_QUERY("selectByQuery", "根据Query查询数据", "<script>SELECT %s FROM %s %s %s</script>"),

    // 字段 表名 where语句 "order by"
    SELECT_BY_QUERY_SPECIFIED_COLUMN("selectByQuerySpecifiedColumn", "根据Query查询数据", "<script>SELECT %s FROM %s %s %s</script>"),

    SELECT_ONE_BY_QUERY("selectOneByQuery", "根据Query查询数据(单条)", "<script>SELECT %s FROM %s %s %s LIMIT 1</script>"),

    SELECT_ONE_BY_QUERY_SPECIFIED_COLUMN("selectOneByQuerySpecifiedColumn", "根据Query查询数据（单条,可指定字段）", "<script>SELECT %s FROM %s %s %s LIMIT 1</script>"),

    COUNT_BY_QUERY("countByQuery", "根据Query查询条数", "<script>SELECT COUNT(0) FROM %s %s</script>"),

    GET_PAGE("getPage", "根据Query查询数据", "<script>SELECT %s FROM %s %s %s</script>"),

    GET_PAGE_SPECIFIED_COLUMN("getPageSpecifiedColumn", "根据Query查询数据", "<script>SELECT %s FROM %s %s %s</script>");


    @Getter
    private String method;
    @Getter
    private String desc;
    @Getter
    private String sql;

}
